package link.anyauto.fsmgr.biz.main;

import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.List;

import link.anyauto.fsmgr.R;
import link.anyauto.fsmgr.databinding.GroupMainBinding;
import link.anyauto.fsmgr.databinding.ItemMainBinding;
import link.anyauto.fsmgr.vo.FunctionGroupVO;
import link.anyauto.fsmgr.vo.FunctionVO;

/**
 * Created by LYQ on 2016/7/19.
 */
public class FunctionsAdapter extends BaseExpandableListAdapter {

    public FunctionsAdapter(List<FunctionGroupVO> groups) {
        this.groups = groups;
    }

    @Override
    public int getGroupCount() {
        return groups == null ? 0 : groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).getFuncs().size();
    }

    @Override
    public FunctionGroupVO getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public FunctionVO getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition).getFuncs().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupMainBinding binding;
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_main, null);
            binding = GroupMainBinding.bind(convertView);
            convertView.setTag(binding);
        } else {
            binding = (GroupMainBinding) convertView.getTag();
        }
        binding.setGroup(getGroup(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemMainBinding binding;
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, null);
            binding = ItemMainBinding.bind(convertView);
            convertView.setTag(binding);
        } else {
            binding = (ItemMainBinding) convertView.getTag();
        }
        binding.setFunction(getChild(groupPosition, childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public List<FunctionGroupVO> groups;
}
